<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="教练姓名" clearable></el-input>
                    <el-input v-model="search.mobile" placeholder="手机号码" clearable></el-input>
                    <el-input v-model="search.agentName" placeholder="所属机构" clearable></el-input>
                    <el-select v-model="search.level" placeholder="等级" clearable>
                        <el-option label="初级" :value="1"></el-option>
                        <el-option label="中级" :value="2"></el-option>
                        <el-option label="高级" :value="3"></el-option>
                    </el-select>
                    <el-select v-model="search.gender" placeholder="性别" clearable>
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="2"></el-option>
                    </el-select>
                    <el-select v-model="search.teachExercise" placeholder="教学练习" clearable>
                        <el-option label="未开通" :value="0"></el-option>
                        <el-option label="开通" :value="1"></el-option>
                    </el-select>
                    <el-select v-model="search.auditStatus" placeholder="考核状态" clearable>
                        <el-option label="未通过" :value="0"></el-option>
                        <el-option label="通过" :value="1"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="禁用" :value="0"></el-option>
                        <el-option label="启用" :value="1"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="table_add">增加</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.teacher.list" remoteSort :params="search" @selection-change="selectionChange" @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="账号" prop="account" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.authAccount ? scope.row.authAccount.account : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="姓名" prop="name" align="center"></el-table-column>
                    <el-table-column label="等级" prop="level" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.level ? scope.row.level.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="体验" prop="teachExercise" width="130" align="center">
                        <template #default="scope">
                             {{ scope.row.teachExercise ? scope.row.teachExercise.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="头衔" prop="title" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.title ? scope.row.title.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="所属机构" prop="agent" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.agent ? scope.row.agent.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="手机号" prop="mobile" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="年龄" prop="age" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="性别" prop="gender" width="100" align="center">
                        <template #default="scope">
                            {{ scope.row.gender ? scope.row.gender.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="考核状态" prop="auditStatus" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.auditStatus ? scope.row.auditStatus.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="120" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="danger">{{scope.row.status.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="200">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row)">查看</el-button>
                            <el-divider direction="vertical" v-if="scope.row.agent.id == userInfo.id"></el-divider>
                            <el-button type="text" size="small" @click="table_edit(scope.row)" v-if="scope.row.agent.id == userInfo.id">编辑</el-button>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" :teacherTitles="teacherTitles" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

    <time-schedule v-if="dialog.timeSchedule" ref="timeSchedule" @closed="dialog.timeSchedule=false"></time-schedule>

</template>

<script>
import saveDialog from './save'
import detailDialog from './detail'
import timeSchedule from './components/timeSchedule.vue'
export default {
    name: 'teacher',
    components: {
        saveDialog,
        detailDialog,
        timeSchedule
    },
    data() {
        return {
            dialog: {
                save: false,
                detail: false,
                timeSchedule: false
            },
            selection: [],
            search: {
                name: null,
            },
            teacherTitles: [],
            userInfo: null
        }
    },
    mounted() {
        this.userInfo = this.$TOOL.data.get("G_USER_INFO");
        this.getAllTeacherTitle()
    },
    methods: {
        getAllTeacherTitle() {
            this.$API.teacher.title.all.get({}).then(res => {
                this.teacherTitles = res.data
            })
        },
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        // 增加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        // 编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id, 'first')
			})
        },
        //时间安排
        table_time(row) {
            this.dialog.timeSchedule = true
			this.$nextTick(() => {
				this.$refs.timeSchedule.open(row)
			})
        },
        //状态变更
        async table_status(row, status) {
            await this.$API.teacher.updateStatus.put({ id: row.id, status: status }).then(() => {
                this.$notify.success("更新成功")
                this.$refs.table.refresh()
            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
</style>
